package cn.yuan.org.controller;

import cn.yuan.basic.util.PageList;
import cn.yuan.org.domain.Employee;
import cn.yuan.org.query.EmployeeQuery;
import cn.yuan.org.service.IEmployeeService;
import cn.yuan.util.AjaxResult.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "员工查找",notes = "根据ID查找员工")
    public Employee loadById(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有员工",notes = "根据ID查找员工")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "根据ID删除员工")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除员工",notes = "根据ID删除员工")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        System.out.println(ids);
        try {
            employeeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改员工",notes = "添加或者修改员工")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "查询分页",notes = "分页查询员工")
    public PageList<Employee> queryForPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryForPage(employeeQuery);
    }
}
